package com.zmx.doctor.view.activity;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.wang.adapters.interfaceabstract.OnItemClickListener;
import com.zmx.doctor.BaseActivity;
import com.zmx.doctor.R;
import com.zmx.doctor.adapter.GroupingAdapter;
import com.zmx.doctor.okhttp.OkHttp3ClientManager;
import com.zmx.doctor.pojo.Grouping;
import com.zmx.doctor.utils.AllUtil;
import com.zmx.doctor.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GroupingActivity extends BaseActivity {


    @BindView(R.id.add_button)
    ImageButton addButton;
    private List<Grouping> lists;
    private GroupingAdapter adapter;
    private RecyclerView reView;

    private String value;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_grouping;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);

        lists = new ArrayList<>();
        reView = findViewById(R.id.reView);
        reView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new GroupingAdapter(this);
        reView.setAdapter(adapter);

        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            protected void onItemClick(View view, int listPosition) {
                alert_label(lists.get(listPosition).getName(),lists.get(listPosition).getId()+"");

            }
        });


        group();
    }

    public void alert_label(String text,String g_id) {

        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);

        LayoutInflater factory = LayoutInflater.from(this);//提示框
        final View view = factory.inflate(R.layout.dialog_edit, null);//这里必须是final的
        final EditText et = view.findViewById(R.id.editText);
        et.setHint(text);
        builder.setTitle(text);

        final AlertDialog dialog = builder.create();
        dialog.setView(view);

        dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();

            }
        });
        dialog.show();
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (!TextUtils.isEmpty(et.getText().toString())) {

                    value = et.getText().toString();
                    config(et.getText().toString(),g_id);
                    dialog.dismiss();

                } else {

                    Toast("非法输入!");

                }


            }
        });


    }


    @Override
    protected void onRestart() {
        super.onRestart();

        group();

    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 1:

                    Log.e("方法是", "发生发" + msg.obj.toString());

                    try {
                        lists.clear();
                        JSONObject object = new JSONObject(msg.obj.toString());
                        JSONArray array = object.getJSONArray("data");
                        Gson gson = new Gson();
                        for (int i = 0; i < array.length(); i++) {

                            Grouping g = gson.fromJson(array.getJSONObject(i).toString(), Grouping.class);
                            lists.add(g);

                        }
                        adapter.setListAndNotifyDataSetChanged(lists);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                    break;
                    case 2:

                    Log.e("方法是", "发生发" + msg.obj.toString());

                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());
                        Toast(object.getString("msg"));

                        if (object.getInt("code") == 1){

                            group();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                    break;

            }

        }
    };


    public void group() {

        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(null)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "group");
        params.put("data[]", "");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/doctor/", params, handler, 1, 404);

    }



    @OnClick(R.id.add_button)
    public void onViewClicked() {

        Intent intent = new Intent();
        intent.setClass(mActivity, AddGroupingActivity.class);
        startActivity(intent);

    }




    public void config(String name, String group_id) {

        List<String> l = new ArrayList<>();
        l.add(group_id);
        l.add(name);

        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(l)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "editGroup");
        params.put("data[group_id]", group_id);
        params.put("data[name]", name);
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/doctor/", params, handler, 2, 404);

    }
}
